package org.apache.ocean.main.http;

import java.io.IOException;
import java.net.URL;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.ocean.main.util.Timeout.TimeoutException;

/**
 * 
 * @author jasonr
 */
public class HttpRequest { 
	public String method;
	public URL url;
	public Object attachment;
	public HttpTimeout httpTimeout;
	public HttpParameters parameters;
	public byte[] body;
	public String bodyContentType;

	public HttpRequest(URL url) {
		this.url = url;
	}

	public HttpRequest(Object attachment, URL url) {
		this.attachment = attachment;
		this.url = url;
	}

	public HttpRequest(Object attachment, URL url, HttpParameters parameters) {
		this.attachment = attachment;
		this.url = url;
		this.parameters = parameters;
	}

	public HttpRequest() {
	}

	public String toString() {
		return ReflectionToStringBuilder.toString(this);
	}

	public static HttpResponse post(URL url, String bodyContentType, byte[] body, HttpTimeout httpTimeout) throws Exception {
		HttpRequest httpRequest = new HttpRequest();
		httpRequest.url = url;
		httpRequest.method = "post";
		httpRequest.body = body;
		httpRequest.bodyContentType = bodyContentType;
		httpRequest.httpTimeout = httpTimeout;
		return CHttpClient.request(httpRequest);
	}

	public static HttpResponse get(URL url, HttpParameters parameters, HttpTimeout httpTimeout) throws TimeoutException, IOException {
		HttpRequest httpRequest = new HttpRequest();
		httpRequest.url = url;
		httpRequest.method = "get";
		httpRequest.parameters = parameters;
		httpRequest.httpTimeout = httpTimeout;
		return CHttpClient.request(httpRequest);  
	}
  
	public static HttpResponse get(URL url) throws TimeoutException, IOException {  
		HttpRequest httpRequest = new HttpRequest(); 
		httpRequest.url = url; 
		httpRequest.method = "get";
		return CHttpClient.request(httpRequest);
	}
	
	public static HttpResponse get(URL url, HttpTimeout httpTimeout) throws TimeoutException, IOException {
		HttpRequest httpRequest = new HttpRequest();
		httpRequest.url = url;
		httpRequest.method = "get";
		httpRequest.httpTimeout = httpTimeout;
		return CHttpClient.request(httpRequest);
	}
}
